package com.itheima.response;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 重定向的使用
 */

@WebServlet("/ResponseDemo1")
public class ResponseDemo1 extends HttpServlet {

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("response1....");
        //重定向的步骤
        //1.设置状态码
//        response.setStatus(302);//浏览器看到302就知道要进行重定向操作
//        //2.设置转发路径(发送给浏览器的)
//        response.setHeader("location","/web-demo/ResponseDemo2");//高版本的tomcat是不需要加上虚拟路径的

        //重定向的简化写法
//        response.sendRedirect("/web-demo/ResponseDemo2");

        //使用动态获取虚拟路径的方法减少重定向和虚拟路径之间的耦合性
        //1.获取虚拟路径
        String contextPath = request.getContextPath();
        response.sendRedirect(contextPath+"/ResponseDemo2");
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }
}